package com.xbongbong.saas.help;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.saas.enums.ShowTypeEnum;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author hiabin.zhang
 * @version v1.0
 * @date 2019/1/20 16:14
 * @since v1.0
 */
@Component
public class FieldAttrHelp {
    /**
     * 获取一个基本的开关字段解释
     *
     * @return 开关字段解释
     */
    public FieldAttrEntity getBaseSwitchFiledAttr() {
        FieldAttrEntity fieldAttrEntity = new FieldAttrEntity();
        fieldAttrEntity.setFieldType(FieldTypeEnum.SWITCH.getType());
        return fieldAttrEntity;
    }

    /**
     * 获取一个基本的下拉字段解释
     *
     * @param defaultItem 默认的下拉选项
     * @return 开关字段解释
     */
    public FieldAttrEntity getBaseCombFiledAttr(String defaultItem) {
        FieldAttrEntity fieldAttrEntity = new FieldAttrEntity();
        fieldAttrEntity.setFieldType(FieldTypeEnum.COMBO.getType());
        initItem(fieldAttrEntity, defaultItem);
        return fieldAttrEntity;
    }

    /**
     * 下拉选项初始化
     * @param defaultItem
     * @return
     */
    public List<ItemPoJo> initItem(String defaultItem){
        List<String> stringList;
        try {
            stringList = StringUtil.toStringList(defaultItem, ",");
            if (stringList == null) {
                return new ArrayList<>();
            }
        } catch (Exception e) {
            return new ArrayList<>();
        }
        List<ItemPoJo> itemPoJoList = new ArrayList<>();
        Integer value = 1;
        for (String text : stringList) {
            ItemPoJo itemPoJo = new ItemPoJo();
            itemPoJo.setText(text);
            itemPoJo.setValue(value.toString());
            itemPoJo.setChecked(false);
            itemPoJoList.add(itemPoJo);
            value++;
        }
        return itemPoJoList;
    }


    /**
     * 下拉默认选项
     *
     * @param fieldAttrEntity 字段解释
     * @param defaultItem     默认下拉选项str
     */
    private void initItem(FieldAttrEntity fieldAttrEntity, String defaultItem) {
        fieldAttrEntity.setItems(initItem(defaultItem));
    }

    /**
     * 根据字段实体判断字段在新建/编辑页面是否展示
     *
     * @param showType 展示类型
     * @return true：展示；false：不展示
     */
    public static boolean editShow(Integer showType) {
        if (showType == null) {
            return false;
        }
        if (Objects.equals(showType, ShowTypeEnum.ALL.getCode()) ||
            Objects.equals(showType, ShowTypeEnum.EDIT.getCode()) ||
            Objects.equals(showType, ShowTypeEnum.EDIT_LIST.getCode()) ||
            Objects.equals(showType, ShowTypeEnum.EDIT_DETAIL.getCode())
            ) {
            return true;
        }
        return false;
    }
    /**
     * 根据字段实体判断字段在列表页面是否展示
     *
     * @param showType 展示类型
     * @return true：展示；false：不展示
     */
    public static boolean listShow(Integer showType) {
        if (showType == null) {
            return false;
        }
        if (Objects.equals(showType, ShowTypeEnum.ALL.getCode()) ||
            Objects.equals(showType, ShowTypeEnum.LIST.getCode()) ||
            Objects.equals(showType, ShowTypeEnum.EDIT_LIST.getCode()) ||
            Objects.equals(showType, ShowTypeEnum.LIST_DETAIL.getCode())
            ) {
            return true;
        }
        return false;
    }
    /**
     * 根据字段实体判断字段在详情页面是否展示
     *
     * @param showType 展示类型
     * @return true：展示；false：不展示
     */
    public static boolean detailShow(Integer showType) {
        if (showType == null) {
            return false;
        }
        if (Objects.equals(showType, ShowTypeEnum.ALL.getCode()) ||
            Objects.equals(showType, ShowTypeEnum.DETAIL.getCode()) ||
            Objects.equals(showType, ShowTypeEnum.EDIT_DETAIL.getCode()) ||
            Objects.equals(showType, ShowTypeEnum.LIST_DETAIL.getCode())
            ) {
            return true;
        }
        return false;
    }

    /**
     * 通过数据字典的value值获取对应的text
     * @author lcx
     * @date 2019/3/2 11:45
     * @param unitKey 数据字典key值
     * @param fieldAttrEntity 字段解释实体
     */
    public String getDataDictionaryTextByValue(String unitKey, FieldAttrEntity fieldAttrEntity) {
        JSONObject json = new JSONObject();
        json.put(fieldAttrEntity.getAttr(),unitKey);
        Object value = ItemUtil.getCustomItemList(fieldAttrEntity,json);
        return value == null ? "" : value.toString();
//        if (unitKey == null || fieldAttrEntity == null) {
//            return "";
//        }
//        String unitStr = "";
//        List<ItemPoJo> items = fieldAttrEntity.getItems();
//        if (items != null && !items.isEmpty()) {
//            for (ItemPoJo itemPoJo : items) {
//                String value = itemPoJo.getValue().toString();
//                if (value.equals(unitKey)) {
//                    unitStr = itemPoJo.getText();
//                    return unitStr;
//                }
//            }
//        }
//        if (!ItemUtil.isUUID(unitKey)) {
//            unitStr = unitKey;
//        }
//        return unitStr;
    }
}
